package io.jsen.meta.diff.modifier

import com.google.common.collect.Maps
import com.google.common.reflect.TypeToken
import io.jsen.meta.diff.obj.MetaObject
import io.jsen.meta.diff.obj.special.MetaDelete
import io.jsen.meta.diff.support.MetaChangeModifier
import io.jsen.meta.diff.watch.MetaPath
import io.jsen.meta.diff.watch.MetaPathSearch
import spock.lang.Specification
import uk.co.jemos.podam.api.PodamFactory
import uk.co.jemos.podam.api.PodamFactoryImpl

import static io.jsen.meta.diff.context.DefaultMetaContext.instance
/**
 *
 * 状态测试
 * @author jsen*
 * @date 2021/09/18
 * */
class MetaDiffModifierTest extends Specification {
    PodamFactory factory = new PodamFactoryImpl()

    def "基础类型删除"() {

    }

    def "list内部对象删除"() {

    }

    def "list整体删除"() {

    }

    def "map内部对象删除"() {

    }

    def "map整体删除"() {
        given:
        Map<String, SimpleObject> oldMap = Maps.newHashMap()
        Map<String, SimpleObject> newMap = Maps.newHashMap()
        SimpleObject oldPojo = factory.manufacturePojo(SimpleObject.class)
        oldPojo.setIndex("xxlx")
        oldMap["xxlx"] = oldPojo
        TypeToken typeToken = new TypeToken<Map<String, SimpleObject>>() {}
        when:
        MetaObject obj = instance.diffMeta(typeToken, newMap, oldMap)
        instance.toNative(obj)
        MetaObject i1 = MetaPathSearch.search(obj, MetaPath.builder(true).p("xxlx").build())
        then:
        MetaChangeModifier.isDelete(obj.getModifier())
        i1 instanceof MetaDelete
    }

    def "对象直接删除"() {
        given:
        SimpleObject oldPojo = factory.manufacturePojo(SimpleObject.class)
        SimpleObject newPojo = factory.manufacturePojo(SimpleObject.class)
        oldPojo.setIndex("1")
        newPojo.setIndex("1")
        when:
        oldPojo.setName(oldName)
        newPojo.setName(newName)
        MetaObject obj = instance.diffMeta(TypeToken.of(SimpleObject), newPojo, oldPojo)
        then:
        MetaChangeModifier.isDelete(obj.getModifier()) == isDel
        MetaChangeModifier.isChange(obj.getModifier()) == isChange
        where:
        oldName || newName    | isDel || isChange
        "删除名字"    || "m2"       | false || true
        "m2"    || "m2"       | false || false
        "m2"    || "删除名字"     | true  || false
        "m2"    || "名字已经被删除了" | false || true
    }

    def "对象字段删除"() {
        given:
        ComplexObject oldPojo = factory.manufacturePojo(ComplexObject.class)
        ComplexObject newPojo = factory.manufacturePojo(ComplexObject.class)
        oldPojo.setIndex("1")
        newPojo.setIndex("1")
        when:
        oldPojo.setName(oldName)
        newPojo.setName(newName)
        MetaObject obj = instance.diffMeta(TypeToken.of(ComplexObject), newPojo, oldPojo)
        then:
        MetaChangeModifier.isDelete(obj.getModifier()) == isDel
        MetaChangeModifier.isChange(obj.getModifier()) == isChange
        where:
        oldName || newName | isDel || isChange
        "m2"    || "删除名字"  | false || true
    }
}